package de.arnoldmedia.mybookshelf.connector;

import java.text.MessageFormat;

import com.skype.ApplicationAdapter;
import com.skype.SkypeException;
import com.skype.Stream;

import de.arnoldmedia.mybookshelf.IMyBookshelf;
import de.arnoldmedia.mybookshelf.resources.Constants;
import de.arnoldmedia.mybookshelf.resources.ResourceHandler;

/**
 * Register to Skype an listen for incoming events
 * 
 * @author aarnold
 * @date 21.02.2009
 * @version 1.0
 */
public class SkypeListener extends ApplicationAdapter {

	private IMyBookshelf mainApp = null;
	private ResourceHandler resourceHandler = null;
	private SkypeConnector parent = null;
	
	/**
	 * @category constructor
	 * @param parent
	 */
	public SkypeListener(IMyBookshelf ma, SkypeConnector p, ResourceHandler rH) {

		// set reference to calling application
		this.mainApp = ma;
		this.parent = p;
		this.resourceHandler = rH;

	}// SkypeListener

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.skype.ApplicationAdapter#connected(com.skype.Stream)
	 */
	@Override
	public void connected(Stream sm) throws SkypeException {

		SkypeStreamAdapter ssa = new SkypeStreamAdapter(this.mainApp, sm);
		
		// overwrite event listener and provide action in case of new message is
		// Received
		sm.addStreamListener( ssa );// addStreamListener

		// TODO replace println
		// "connection established with "{0}" (client ID {1})"
		System.out.println(MessageFormat.format( this.resourceHandler
				.getPropertyString(Constants.I18N_CLIENT_CONNECTED), 
				sm.getApplication().getName(), sm.getId()));

	}// connected

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.skype.ApplicationAdapter#disconnected(com.skype.Stream)
	 */
	@Override
	public void disconnected(Stream sm) throws SkypeException {

		// TODO replace println
		// "connection lost with "{0}" (client ID {1})"
		System.out.println(MessageFormat.format( this.resourceHandler
				.getPropertyString(Constants.I18N_CLIENT_DISCONNECTED), 
				sm.getApplication().getName(), sm.getId()));

	}// disconnected

}// class
